home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / stdio / vfscanf.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  18KB  |  754 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Chris Torek.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)vfscanf.c    5.6 (Berkeley) 2/24/91";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #define KERNEL
  42. #include "ixemul.h"
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <ctype.h>
  47. #include <stdarg.h>
  48. #include "local.h"
  49.  
  50. #undef BUF
  51.  
  52. #define FLOATING_POINT
  53.  
  54. #ifdef FLOATING_POINT
  55. #include "floatio.h"
  56. #define    BUF    (MAXEXP+MAXFRACT+3)    /* 3 = sign + decimal point + NUL */
  57. #else
  58. #define    BUF    40
  59. #endif
  60.  
  61. /*
  62.  * Flags used during conversion.
  63.  */
  64. #define    LONG        0x01    /* l: long or double */
  65. #define    LONGDBL        0x02    /* L: long double; unimplemented */
  66. #define    SHORT        0x04    /* h: short */
  67. #define    SUPPRESS    0x08    /* suppress assignment */
  68. #define    POINTER        0x10    /* weird %p pointer (`fake hex') */
  69. #define    NOSKIP        0x20    /* do not skip blanks */
  70.  
  71. /*
  72.  * The following are used in numeric conversions only:
  73.  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
  74.  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
  75.  */
  76. #define    SIGNOK        0x40    /* +/- is (still) legal */
  77. #define    NDIGITS        0x80    /* no digits detected */
  78.  
  79. #define    DPTOK        0x100    /* (float) decimal point is still legal */
  80. #define    EXPOK        0x200    /* (float) exponent (e+3, etc) still legal */
  81.  
  82. #define    PFXOK        0x100    /* 0x prefix is (still) legal */
  83. #define    NZDIGITS    0x200    /* no zero digits detected */
  84.  
  85. /*
  86.  * Conversion types.
  87.  */
  88. #define    CT_CHAR        0    /* %c conversion */
  89. #define    CT_CCL        1    /* %[...] conversion */
  90. #define    CT_STRING    2    /* %s conversion */
  91. #define    CT_INT        3    /* integer, i.e., strtol or strtoul */
  92. #define    CT_FLOAT    4    /* floating, i.e., strtod */
  93.  
  94. #define u_char unsigned char
  95. #define u_long unsigned long
  96.  
  97. static u_char *__sccl();
  98.  
  99. /*
  100.  * vfscanf
  101.  */
  102. int __svfscanf(FILE *fp, char const *fmt0, va_list ap)
  103. {
  104.     register u_char *fmt = (u_char *)fmt0;
  105.     register int c;        /* character from format, or conversion */
  106.     register size_t width;    /* field width, or 0 */
  107.     register char *p;    /* points into all kinds of strings */
  108.     register int n;        /* handy integer */
  109.     register int flags;    /* flags as defined above */
  110.     register char *p0;    /* saves original value of p when necessary */
  111.     int nassigned;        /* number of fields assigned */
  112.     int nread;        /* number of characters consumed from fp */
  113.     int base;        /* base argument to strtol/strtoul */
  114.     u_long (*ccfn)();    /* conversion function (strtol/strtoul) */
  115.     char ccltab[256];    /* character class table for %[...] */
  116.     char buf[BUF];        /* buffer for numeric conversions */
  117.  
  118.     /* `basefix' is used to avoid `if' tests in the integer scanner */
  119.     static const short basefix[17] =
  120.         { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  121.  
  122.     if (!fp)
  123.       return 0;
  124.  
  125.     nassigned = 0;
  126.     nread = 0;
  127.     base = 0;        /* XXX just to keep gcc happy */
  128.     ccfn = NULL;        /* XXX just to keep gcc happy */
  129.     for (;;) {
  130.         c = *fmt++;
  131.         if (c == 0)
  132.             return (nassigned);
  133.         if (isspace(c)) {
  134.             for (;;) {
  135.                 if (fp->_r <= 0 && __srefill(fp))
  136.                     return (nassigned);
  137.                 if (!isspace(*fp->_p))
  138.                     break;
  139.                 nread++, fp->_r--, fp->_p++;
  140.             }
  141.             continue;
  142.         }
  143.         if (c != '%')
  144.             goto literal;
  145.         width = 0;
  146.         flags = 0;
  147.         /*
  148.          * switch on the format.  continue if done;
  149.          * break once format type is derived.
  150.          */
  151. again:        c = *fmt++;
  152.         switch (c) {
  153.         case '%':
  154. literal:
  155.             if (fp->_r <= 0 && __srefill(fp))
  156.                 goto input_failure;
  157.             if (*fp->_p != c)
  158.                 goto match_failure;
  159.             fp->_r--, fp->_p++;
  160.             nread++;
  161.             continue;
  162.  
  163.         case '*':
  164.             flags |= SUPPRESS;
  165.             goto again;
  166.         case 'l':
  167.             flags |= LONG;
  168.             goto again;
  169.         case 'L':
  170.             flags |= LONGDBL;
  171.             goto again;
  172.         case 'h':
  173.             flags |= SHORT;
  174.             goto again;
  175.  
  176.         case '0': case '1': case '2': case '3': case '4':
  177.         case '5': case '6': case '7': case '8': case '9':
  178.             width = width * 10 + c - '0';
  179.             goto again;
  180.  
  181.         /*
  182.          * Conversions.
  183.          * Those marked `compat' are for 4.[123]BSD compatibility.
  184.          *
  185.          * (According to ANSI, E and X formats are supposed
  186.          * to the same as e and x.  Sorry about that.)
  187.          */
  188.         case 'D':    /* compat */
  189.             flags |= LONG;
  190.             /* FALLTHROUGH */
  191.         case 'd':
  192.             c = CT_INT;
  193.             ccfn = (u_long (*)())strtol;
  194.             base = 10;
  195.             break;
  196.  
  197.         case 'i':
  198.             c = CT_INT;
  199.             ccfn = (u_long (*)())strtol;
  200.             base = 0;
  201.             break;
  202.  
  203.         case 'O':    /* compat */
  204.             flags |= LONG;
  205.             /* FALLTHROUGH */
  206.         case 'o':
  207.             c = CT_INT;
  208.             ccfn = strtoul;
  209.             base = 8;
  210.             break;
  211.  
  212.         case 'u':
  213.             c = CT_INT;
  214.             ccfn = strtoul;
  215.             base = 10;
  216.             break;
  217.  
  218.         case 'X':    /* compat   XXX */
  219.             flags |= LONG;
  220.             /* FALLTHROUGH */
  221.         case 'x':
  222.             flags |= PFXOK;    /* enable 0x prefixing */
  223.             c = CT_INT;
  224.             ccfn = strtoul;
  225.             base = 16;
  226.             break;
  227.  
  228. #ifdef FLOATING_POINT
  229.         case 'E':    /* compat   XXX */
  230.         case 'F':    /* compat */
  231.             flags |= LONG;
  232.             /* FALLTHROUGH */
  233.         case 'e': case 'f': case 'g':
  234.             c = CT_FLOAT;
  235.             break;
  236. #endif
  237.  
  238.         case 's':
  239.             c = CT_STRING;
  240.             break;
  241.  
  242.         case '[':
  243.             fmt = __sccl(ccltab, fmt);
  244.             flags |= NOSKIP;
  245.             c = CT_CCL;
  246.             break;
  247.  
  248.         case 'c':
  249.             flags |= NOSKIP;
  250.             c = CT_CHAR;
  251.             break;
  252.  
  253.         case 'p':    /* pointer format is like hex */
  254.             flags |= POINTER | PFXOK;
  255.             c = CT_INT;
  256.             ccfn = strtoul;
  257.             base = 16;
  258.             break;
  259.  
  260.         case 'n':
  261.             if (flags & SUPPRESS)    /* ??? */
  262.                 continue;
  263.             if (flags & SHORT)
  264.                 *va_arg(ap, short *) = nread;
  265.             else if (flags & LONG)
  266.                 *va_arg(ap, long *) = nread;
  267.             else
  268.                 *va_arg(ap, int *) = nread;
  269.             continue;
  270.  
  271.         /*
  272.          * Disgusting backwards compatibility hacks.    XXX
  273.          */
  274.         case '\0':    /* compat */
  275.             return (EOF);
  276.  
  277.         default:    /* compat */
  278.             if (isupper(c))
  279.                 flags |= LONG;
  280.             c = CT_INT;
  281.             ccfn = (u_long (*)())strtol;
  282.             base = 10;
  283.             break;
  284.         }
  285.  
  286.         /*
  287.          * We have a conversion that requires input.
  288.          */
  289.         if (fp->_r <= 0 && __srefill(fp))
  290.             goto input_failure;
  291.  
  292.         /*
  293.          * Consume leading white space, except for formats
  294.          * that suppress this.
  295.          */
  296.         if ((flags & NOSKIP) == 0) {
  297.             while (isspace(*fp->_p)) {
  298.                 nread++;
  299.                 if (--fp->_r > 0)
  300.                     fp->_p++;
  301.                 else if (__srefill(fp))
  302.                     goto input_failure;
  303.             }
  304.             /*
  305.              * Note that there is at least one character in
  306.              * the buffer, so conversions that do not set NOSKIP
  307.              * ca no longer result in an input failure.
  308.              */
  309.         }
  310.  
  311.         /*
  312.          * Do the conversion.
  313.          */
  314.         switch (c) {
  315.  
  316.         case CT_CHAR:
  317.             /* scan arbitrary characters (sets NOSKIP) */
  318.             if (width == 0)
  319.                 width = 1;
  320.             if (flags & SUPPRESS) {
  321.                 size_t sum = 0;
  322.                 for (;;) {
  323.                     if ((n = fp->_r) < width) {
  324.                         sum += n;
  325.                         width -= n;
  326.                         fp->_p += n;
  327.                         if (__srefill(fp)) {
  328.                             if (sum == 0)
  329.                                 goto input_failure;
  330.                             break;
  331.                         }
  332.                     } else {
  333.                         sum += width;
  334.                         fp->_r -= width;
  335.                         fp->_p += width;
  336.                         break;
  337.                     }
  338.                 }
  339.                 nread += sum;
  340.             } else {
  341.                 size_t r = fread((void *)va_arg(ap, char *), 1,
  342.                     width, fp);
  343.  
  344.                 if (r == 0)
  345.                     goto input_failure;
  346.                 nread += r;
  347.                 nassigned++;
  348.             }
  349.             break;
  350.  
  351.         case CT_CCL:
  352.             /* scan a (nonempty) character class (sets NOSKIP) */
  353.             if (width == 0)
  354.                 width = ~0;    /* `infinity' */
  355.             /* take only those things in the class */
  356.             if (flags & SUPPRESS) {
  357.                 n = 0;
  358.                 while (ccltab[*fp->_p]) {
  359.                     n++, fp->_r--, fp->_p++;
  360.                     if (--width == 0)
  361.                         break;
  362.                     if (fp->_r <= 0 && __srefill(fp)) {
  363.                         if (n == 0)
  364.                             goto input_failure;
  365.                         break;
  366.                     }
  367.                 }
  368.                 if (n == 0)
  369.                     goto match_failure;
  370.             } else {
  371.                 p0 = p = va_arg(ap, char *);
  372.                 while (ccltab[*fp->_p]) {
  373.                     fp->_r--;
  374.                     *p++ = *fp->_p++;
  375.                     if (--width == 0)
  376.                         break;
  377.                     if (fp->_r <= 0 && __srefill(fp)) {
  378.                         if (p == p0)
  379.                             goto input_failure;
  380.                         break;
  381.                     }
  382.                 }
  383.                 n = p - p0;
  384.                 if (n == 0)
  385.                     goto match_failure;
  386.                 *p = 0;
  387.                 nassigned++;
  388.             }
  389.             nread += n;
  390.             break;
  391.  
  392.         case CT_STRING:
  393.             /* like CCL, but zero-length string OK, & no NOSKIP */
  394.             if (width == 0)
  395.                 width = ~0;
  396.             if (flags & SUPPRESS) {
  397.                 n = 0;
  398.                 while (!isspace(*fp->_p)) {
  399.                     n++, fp->_r--, fp->_p++;
  400.                     if (--width == 0)
  401.                         break;
  402.                     if (fp->_r <= 0 && __srefill(fp))
  403.                         break;
  404.                 }
  405.                 nread += n;
  406.             } else {
  407.                 p0 = p = va_arg(ap, char *);
  408.                 while (!isspace(*fp->_p)) {
  409.                     fp->_r--;
  410.                     *p++ = *fp->_p++;
  411.                     if (--width == 0)
  412.                         break;
  413.                     if (fp->_r <= 0 && __srefill(fp))
  414.                         break;
  415.                 }
  416.                 *p = 0;
  417.                 nread += p - p0;
  418.                 nassigned++;
  419.             }
  420.             continue;
  421.  
  422.         case CT_INT:
  423.             /* scan an integer as if by strtol/strtoul */
  424. #ifdef hardway
  425.             if (width == 0 || width > sizeof(buf) - 1)
  426.                 width = sizeof(buf) - 1;
  427. #else
  428.             /* size_t is unsigned, hence this optimisation */
  429.             if (--width > sizeof(buf) - 2)
  430.                 width = sizeof(buf) - 2;
  431.             width++;
  432. #endif
  433.             flags |= SIGNOK | NDIGITS | NZDIGITS;
  434.             for (p = buf; width; width--) {
  435.                 c = *fp->_p;
  436.                 /*
  437.                  * Switch on the character; `goto ok'
  438.                  * if we accept it as a part of number.
  439.                  */
  440.                 switch (c) {
  441.  
  442.                 /*
  443.                  * The digit 0 is always legal, but is
  444.                  * special.  For %i conversions, if no
  445.                  * digits (zero or nonzero) have been
  446.                  * scanned (only signs), we will have
  447.                  * base==0.  In that case, we should set
  448.                  * it to 8 and enable 0x prefixing.
  449.                  * Also, if we have not scanned zero digits
  450.                  * before this, do not turn off prefixing
  451.                  * (someone else will turn it off if we
  452.                  * have scanned any nonzero digits).
  453.                  */
  454.                 case '0':
  455.                     if (base == 0) {
  456.                         base = 8;
  457.                         flags |= PFXOK;
  458.                     }
  459.                     if (flags & NZDIGITS)
  460.                         flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
  461.                     else
  462.                         flags &= ~(SIGNOK|PFXOK|NDIGITS);
  463.                     goto ok;
  464.  
  465.                 /* 1 through 7 always legal */
  466.                 case '1': case '2': case '3':
  467.                 case '4': case '5': case '6': case '7':
  468.                     base = basefix[base];
  469.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  470.                     goto ok;
  471.  
  472.                 /* digits 8 and 9 ok iff decimal or hex */
  473.                 case '8': case '9':
  474.                     base = basefix[base];
  475.                     if (base <= 8)
  476.                         break;    /* not legal here */
  477.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  478.                     goto ok;
  479.  
  480.                 /* letters ok iff hex */
  481.                 case 'A': case 'B': case 'C':
  482.                 case 'D': case 'E': case 'F':
  483.                 case 'a': case 'b': case 'c':
  484.                 case 'd': case 'e': case 'f':
  485.                     /* no need to fix base here */
  486.                     if (base <= 10)
  487.                         break;    /* not legal here */
  488.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  489.                     goto ok;
  490.  
  491.                 /* sign ok only as first character */
  492.                 case '+': case '-':
  493.                     if (flags & SIGNOK) {
  494.                         flags &= ~SIGNOK;
  495.                         goto ok;
  496.                     }
  497.                     break;
  498.  
  499.                 /* x ok iff flag still set & 2nd char */
  500.                 case 'x': case 'X':
  501.                     if (flags & PFXOK && p == buf + 1) {
  502.                         base = 16;    /* if %i */
  503.                         flags &= ~PFXOK;
  504.                         goto ok;
  505.                     }
  506.                     break;
  507.                 }
  508.  
  509.                 /*
  510.                  * If we got here, c is not a legal character
  511.                  * for a number.  Stop accumulating digits.
  512.                  */
  513.                 break;
  514.         ok:
  515.                 /*
  516.                  * c is legal: store it and look at the next.
  517.                  */
  518.                 *p++ = c;
  519.                 if (--fp->_r > 0)
  520.                     fp->_p++;
  521.                 else if (__srefill(fp))
  522.                     break;        /* EOF */
  523.             }
  524.             /*
  525.              * If we had only a sign, it is no good; push
  526.              * back the sign.  If the number ends in `x',
  527.              * it was [sign] '0' 'x', so push back the x
  528.              * and treat it as [sign] '0'.
  529.              */
  530.             if (flags & NDIGITS) {
  531.                 if (p > buf)
  532.                     (void) ungetc(*(u_char *)--p, fp);
  533.                 goto match_failure;
  534.             }
  535.             c = ((u_char *)p)[-1];
  536.             if (c == 'x' || c == 'X') {
  537.                 --p;
  538.                 (void) ungetc(c, fp);
  539.             }
  540.             if ((flags & SUPPRESS) == 0) {
  541.                 u_long res;
  542.  
  543.                 *p = 0;
  544.                 res = (*ccfn)(buf, (char **)NULL, base);
  545.                 if (flags & POINTER)
  546.                     *va_arg(ap, void **) = (void *)res;
  547.                 else if (flags & SHORT)
  548.                     *va_arg(ap, short *) = res;
  549.                 else if (flags & LONG)
  550.                     *va_arg(ap, long *) = res;
  551.                 else
  552.                     *va_arg(ap, int *) = res;
  553.                 nassigned++;
  554.             }
  555.             nread += p - buf;
  556.             break;
  557.  
  558. #ifdef FLOATING_POINT
  559.         case CT_FLOAT:
  560.             /* scan a floating point number as if by strtod */
  561. #ifdef hardway
  562.             if (width == 0 || width > sizeof(buf) - 1)
  563.                 width = sizeof(buf) - 1;
  564. #else
  565.             /* size_t is unsigned, hence this optimisation */
  566.             if (--width > sizeof(buf) - 2)
  567.                 width = sizeof(buf) - 2;
  568.             width++;
  569. #endif
  570.             flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
  571.             for (p = buf; width; width--) {
  572.                 c = *fp->_p;
  573.                 /*
  574.                  * This code mimicks the integer conversion
  575.                  * code, but is much simpler.
  576.                  */
  577.                 switch (c) {
  578.  
  579.                 case '0': case '1': case '2': case '3':
  580.                 case '4': case '5': case '6': case '7':
  581.                 case '8': case '9':
  582.                     flags &= ~(SIGNOK | NDIGITS);
  583.                     goto fok;
  584.  
  585.                 case '+': case '-':
  586.                     if (flags & SIGNOK) {
  587.                         flags &= ~SIGNOK;
  588.                         goto fok;
  589.                     }
  590.                     break;
  591.                 case '.':
  592.                     if (flags & DPTOK) {
  593.                         flags &= ~(SIGNOK | DPTOK);
  594.                         goto fok;
  595.                     }
  596.                     break;
  597.                 case 'e': case 'E':
  598.                     /* no exponent without some digits */
  599.                     if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
  600.                         flags =
  601.                             (flags & ~(EXPOK|DPTOK)) |
  602.                             SIGNOK | NDIGITS;
  603.                         goto fok;
  604.                     }
  605.                     break;
  606.                 }
  607.                 break;
  608.         fok:
  609.                 *p++ = c;
  610.                 if (--fp->_r > 0)
  611.                     fp->_p++;
  612.                 else if (__srefill(fp))
  613.                     break;    /* EOF */
  614.             }
  615.             /*
  616.              * If no digits, might be missing exponent digits
  617.              * (just give back the exponent) or might be missing
  618.              * regular digits, but had sign and/or decimal point.
  619.              */
  620.             if (flags & NDIGITS) {
  621.                 if (flags & EXPOK) {
  622.                     /* no digits at all */
  623.                     while (p > buf)
  624.                         ungetc(*(u_char *)--p, fp);
  625.                     goto match_failure;
  626.                 }
  627.                 /* just a bad exponent (e and maybe sign) */
  628.                 c = *(u_char *)--p;
  629.                 if (c != 'e' && c != 'E') {
  630.                     (void) ungetc(c, fp);/* sign */
  631.                     c = *(u_char *)--p;
  632.                 }
  633.                 (void) ungetc(c, fp);
  634.             }
  635.             if ((flags & SUPPRESS) == 0) {
  636.                 double res;
  637.  
  638.                 *p = 0;
  639.                 res = atof(buf);
  640.                 if (flags & LONG)
  641.                     *va_arg(ap, double *) = res;
  642.                 else
  643.                     *va_arg(ap, float *) = res;
  644.                 nassigned++;
  645.             }
  646.             nread += p - buf;
  647.             break;
  648. #endif /* FLOATING_POINT */
  649.         }
  650.     }
  651. input_failure:
  652.     return (nassigned ? nassigned : -1);
  653. match_failure:
  654.     return (nassigned);
  655. }
  656.  
  657. /*
  658.  * Fill in the given table from the scanset at the given format
  659.  * (just after `[').  Return a pointer to the character past the
  660.  * closing `]'.  The table has a 1 wherever characters should be
  661.  * considered part of the scanset.
  662.  */
  663. static u_char *
  664. __sccl(tab, fmt)
  665.     register char *tab;
  666.     register u_char *fmt;
  667. {
  668.     register int c, n, v;
  669.  
  670.     /* first `clear' the whole table */
  671.     c = *fmt++;        /* first char hat => negated scanset */
  672.     if (c == '^') {
  673.         v = 1;        /* default => accept */
  674.         c = *fmt++;    /* get new first char */
  675.     } else
  676.         v = 0;        /* default => reject */
  677.     /* should probably use memset here */
  678.     for (n = 0; n < 256; n++)
  679.         tab[n] = v;
  680.     if (c == 0)
  681.         return (fmt - 1);/* format ended before closing ] */
  682.  
  683.     /*
  684.      * Now set the entries corresponding to the actual scanset
  685.      * to the opposite of the above.
  686.      *
  687.      * The first character may be ']' (or '-') without being special;
  688.      * the last character may be '-'.
  689.      */
  690.     v = 1 - v;
  691.     for (;;) {
  692.         tab[c] = v;        /* take character c */
  693. doswitch:
  694.         n = *fmt++;        /* and examine the next */
  695.         switch (n) {
  696.  
  697.         case 0:            /* format ended too soon */
  698.             return (fmt - 1);
  699.  
  700.         case '-':
  701.             /*
  702.              * A scanset of the form
  703.              *    [01+-]
  704.              * is defined as `the digit 0, the digit 1,
  705.              * the character +, the character -', but
  706.              * the effect of a scanset such as
  707.              *    [a-zA-Z0-9]
  708.              * is implementation defined.  The V7 Unix
  709.              * scanf treats `a-z' as `the letters a through
  710.              * z', but treats `a-a' as `the letter a, the
  711.              * character -, and the letter a'.
  712.              *
  713.              * For compatibility, the `-' is not considerd
  714.              * to define a range if the character following
  715.              * it is either a close bracket (required by ANSI)
  716.              * or is not numerically greater than the character
  717.              * we just stored in the table (c).
  718.              */
  719.             n = *fmt;
  720.             if (n == ']' || n < c) {
  721.                 c = '-';
  722.                 break;    /* resume the for(;;) */
  723.             }
  724.             fmt++;
  725.             do {        /* fill in the range */
  726.                 tab[++c] = v;
  727.             } while (c < n);
  728. #if 1    /* XXX another disgusting compatibility hack */
  729.             /*
  730.              * Alas, the V7 Unix scanf also treats formats
  731.              * such as [a-c-e] as `the letters a through e'.
  732.              * This too is permitted by the standard....
  733.              */
  734.             goto doswitch;
  735. #else
  736.             c = *fmt++;
  737.             if (c == 0)
  738.                 return (fmt - 1);
  739.             if (c == ']')
  740.                 return (fmt);
  741. #endif
  742.             break;
  743.  
  744.         case ']':        /* end of scanset */
  745.             return (fmt);
  746.  
  747.         default:        /* just another character */
  748.             c = n;
  749.             break;
  750.         }
  751.     }
  752.     /* NOTREACHED */
  753. }
  754.